import 'package:flutter/material.dart';
import 'package:flutter_crud_test_amir_rabbani/core/resource/my_colors.dart';
import 'package:flutter_crud_test_amir_rabbani/core/utils/date_format.dart';
import 'package:flutter_crud_test_amir_rabbani/core/widgets/base_getview.dart';
import 'package:flutter_crud_test_amir_rabbani/core/widgets/clickable_container.dart';
import 'package:flutter_crud_test_amir_rabbani/data/local/my_database.dart';
import 'package:flutter_crud_test_amir_rabbani/peresenter/crud/controller/crud_controller.dart';
import 'package:flutter_crud_test_amir_rabbani/routes/app_pages.dart';
import 'package:get/get.dart';

class ShowCustomerPage extends BaseGetView<CrudController> {
  ShowCustomerPage({super.key});

  @override
  Widget buildState(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Customers'),
        actions: [
          TextButton(
            onPressed: controller.addClick,
            child: Text('Add',
                style: textTheme.labelLarge
                    ?.copyWith(color: CustomColors.primary)),
          )
        ],
      ),
      resizeToAvoidBottomInset: true,
      body: SafeArea(child: _customerListview()),
    );
  }

  _customerListview() => StreamBuilder<List<CustomerDaoData>>(
      stream: controller.read(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Center(child: CircularProgressIndicator());
        }
        if (snapshot.hasError) {
          return Center(child: Text('Error: ${snapshot.error}'));
        }
        final customers = snapshot.data ?? [];
        return ListView.builder(
          itemBuilder: (context, index) {
            CustomerDaoData customer = customers[index];
            return ClickableContainer(
                margin: EdgeInsets.only(left: 20, top: 20, right: 20),
                padding: EdgeInsets.all(20),
                borderRadius: BorderRadius.circular(8),
                color: theme.cardTheme.color,
                child: Column(
                  children: [
                    Row(
                      children: [
                        keyValueCard(al.firstName, customer.firstName),
                        keyValueCard(al.lastName, customer.lastName),
                      ],
                    ),
                    SizedBox(height: 10),
                    Row(
                      children: [
                        keyValueCard(al.email, customer.email),
                        keyValueCard(al.phone, customer.phoneNumber),
                      ],
                    ),
                    SizedBox(height: 10),
                    Row(
                      children: [
                        keyValueCard(al.birthDate,
                            customer.dateOfBirth.toCustomFormat()),
                        keyValueCard(al.bank, customer.bankAccountNumber),
                      ],
                    ),
                    SizedBox(height: 10),
                    Row(
                      children: [
                        Expanded(
                            child: Row(
                          children: [
                            Text('${al.actions}: ',
                                style: textTheme.labelLarge),
                            TextButton(
                              onPressed: () => controller.delete(customer.id),
                              child: Text(al.delete,
                                  style: textTheme.labelLarge
                                      ?.copyWith(color: CustomColors.primary)),
                            ),
                            TextButton(
                              onPressed: () =>
                                  controller.editClick(customer.id),
                              child: Text(al.update,
                                  style: textTheme.labelLarge
                                      ?.copyWith(color: CustomColors.primary)),
                            ),
                          ],
                        )),
                      ],
                    ),
                  ],
                ));
          },
          itemCount: customers.length,
        );
      });

  keyValueCard(String key, value) => Expanded(
        child: RichText(
            text: TextSpan(children: [
          TextSpan(text: '$key: ', style: textTheme.labelLarge),
          TextSpan(text: value, style: textTheme.bodySmall),
        ])),
      );
}
